/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
//额外数组
 var rotate1 = function (nums, k) {
   k = k % nums.length
   if (k % nums.length === 0) return nums

   let cur_v = nums[0]
   let cur_i = 0
   let idx = 0
   let to_v = null
   let c = 0
   let start = 0
   while (c < nums.length) {
     let to_i = (cur_i + k) % nums.length
     to_v = nums[to_i]
     nums[to_i] = cur_v
     if (to_i == start) {
       cur_i = ++idx
       start = cur_i
       cur_v = nums[cur_i]
     } else {
       cur_v = to_v
       cur_i = to_i

       // tmp_k -= 1
     }
     c += 1
     // console.log('nums: ', nums, to_i, visted, flag)
   }
   return nums
 }
 //环状替换
 var rotate2 = function (nums, k) {
   k = k % nums.length
   if (k % nums.length === 0) return nums
   let newNums = [...nums]
   for (let i = 0; i < nums.length; i++) {
     nums[(i + k) % nums.length] = newNums[i]
   }
   return nums
 }
 //数组翻转
 /*
nums = "----->-->"; k =3
result = "-->----->";

reverse "----->-->" we can get "<--<-----"
reverse "<--" we can get "--><-----"
reverse "<-----" we can get "-->----->"
 */
var rotate = function(nums, k) {
  const reverse = (nums, start, end) => {
    while (start < end) {
        const temp = nums[start];
        nums[start] = nums[end];
        nums[end] = temp;
        start += 1;
        end -= 1;
    }
  }
  k %= nums.length;
  reverse(nums, 0, nums.length - 1);
  reverse(nums, 0, k - 1);
  reverse(nums, k, nums.length - 1);
};

 // [3,99,-1,-100]
 let nums = [-1, -100, 3, 99],
   k = 2
 nums = [1, 2, 3, 4, 5, 6, 7]
 k = 3
 nums = [1, 2, 3, 4, 5, 6]
 k = 3
 nums = [
   1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
   22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
   41, 42, 43, 44, 45
 ]
 k = 56
 console.log(rotate1(nums, k))
